//===================================================================================
// $$
//
// Coder: Code Milker v1.0
// 0 - EntityName
// 1 - lower
//===================================================================================


namespace App.Data.SqlServer.Repositories {
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using App.Model;

    public class ApplicationRepository : BaseRepository, IApplicationRepository {
        public ApplicationRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork) {
        }
        public void Create(Application application) {
            this.GetDbSet<Application>().Add(application);
            this.UnitOfWork.SaveChanges();
        }

        public Application GetApplication(Guid applicationId) {
            return this.GetDbSet<Application>()
                .Where(c => c.Id == applicationId)
                .Single();
        }

        public void Update(Application updatedApplication) {
            this.GetDbSet<Application>().Attach(updatedApplication);
            this.SetEntityState(updatedApplication, updatedApplication.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid applicationId) {
            Application ApplicationToDelete = this.GetApplication(applicationId);
            this.GetDbSet<Application>().Remove(ApplicationToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IQueryable<Application> Find()
        {
            return this.GetDbSet<Application>();
        }

        public IQueryable<Application> GetAll(Guid companyId)
        {
            return this.GetDbSet<Application>()
                .Include("JobPosition")
                .Include("Resume")
                .Where(i=>i.CompanyId==companyId)
                .OrderByDescending(i=>i.DateApplied);
        }
        public IQueryable<Application> GetAll()
        {
            return this.GetDbSet<Application>();
        }
    }
}
